package bs.app.Client;

import bs.app.Constant.CConfig;
import bs.app.Constant.KeyConstant;
import bs.app.Main.CCanvas;
import bs.app.Position.CPosition;
import bs.app.Res.CRess;
import bs.app.android.Graphics;
import bs.app.android.Image;

public class AppPage1 {
	public ItemApp[][] Item;
	public int xStart, yStart, yStartItem, yEndCanvas;
	public int wscroller, hscroller;
	public int elementItem, row, colum;
	public int yDistanceItem;
	public int flagRow, flagColum;
	public int Cry, AmDuong;
	public final int NumberDivided = 2;
	public int LowerLimit;
	public int WBackGround, HBackGround;
	public boolean PressOnce = false;
	public int iUpDown = 0; // 0 khong thay doi
							// 1: Up
							// 2 : Down
	public int iDUpDown = 0;
	public int iLeftRight = 0; // 0 : khong co
								// 1: left
								// 2: right
	public int iDLeftRight = 0;
	public boolean flag = false;

	final int H_ITEMS = 57;
	final int W_ITEMS = 57;
	final int KC_ITEMS = 20;

	public AppPage1() {
		CRess.LoadSubMenu();
	}

	public void detroy()
	{
		
	}
	public void init(int element, String[][] data, String[] ImageNameLoad,
			Image[] ImgServer, String[] MarkLoadImg) {
		
		
		CCanvas.s_Instan.connect.isPainting = false;
		elementItem = element;
		wscroller = 8;

		// Five begin
		int iCol = CCanvas.s_iWcanvas / (W_ITEMS + KC_ITEMS);
		int iRow = CCanvas.s_iHcanvas / (H_ITEMS + KC_ITEMS);
		
				
		
		// System.out.println("iCol:"+ iCol + " iRow:"+iRow);
		int iXTemp = ((CCanvas.s_iWcanvas - iCol * W_ITEMS - (iCol - 1)
				* KC_ITEMS)) / 2;
		// System.out.println("iXTemp:"+ iXTemp);
		// Five end
		xStart = iXTemp;
		yStart = 0;
		yDistanceItem = H_ITEMS + KC_ITEMS + 6;
		yEndCanvas = CCanvas.s_iHcanvas;
		WBackGround = CCanvas.s_iWcanvas;
		HBackGround = CCanvas.s_iHcanvas;

		yStartItem = CConfig.HIEGHT_TITLE + 15;
		LowerLimit = yStartItem
				+ (((CCanvas.s_iHcanvas - yStartItem) / yDistanceItem) * yDistanceItem)
				- 2;
		// System.out.println("LowerLimit:"+ LowerLimit);
		colum = iCol;
		row = elementItem / colum;
		if (elementItem % colum != 0)
			row++;
//
//		CCanvas.roll.setViewer(0, CConfig.HIEGHT_SUB_TITLE, CCanvas.s_iWcanvas, row*(H_ITEMS + KC_ITEMS));
//		CCanvas.roll.viewH = row*(H_ITEMS + KC_ITEMS);
//		
		CRess.LoadAppPage1(elementItem, colum, ImageNameLoad, ImgServer,
				MarkLoadImg);
		DetectScreenItem(CCanvas.s_iWcanvas, HBackGround, WBackGround, data);
		iUpDown = 0;
		iDUpDown = 0;
		iLeftRight = 0;
		iDLeftRight = 0;
	}

	public void paintItemApp(Graphics g) {
		g.setClip1(0, CConfig.HIEGHT_TITLE, CCanvas.s_iWcanvas,
				CCanvas.s_iHcanvas - CConfig.HIEGHT_COMMNAND);
		for (int i = 0; i < elementItem; i++) {
			if (CCanvas.CurrentScreen != CCanvas.AppPage1)
				return;
			if (Item[i / colum][i % colum] != null) {
				// if (CCanvas.s_Instan.ispaint(g.getClipX(), g.getClipY(),
				// g.getClipWidth(), g.getClipHeight(), Item[i / colum][i %
				// colum].x, Item[i / colum][i % colum].y,
				// Item[i / colum][i % colum].w, Item[i / colum][i % colum].h))
				{
					Item[i / colum][i % colum].Paint(g);
				}
			}
		}
		g.restoreClip();
	}

	public void paintBackGround(Graphics g) {
		g.setColor(0x000000);
		g.fillRect(0, 0, CCanvas.s_iWcanvas, CCanvas.s_iHcanvas);
		g.drawImage(CRess.background, CCanvas.s_iWcanvas >> 1,
				CCanvas.s_iHcanvas >> 1, CPosition.ANCHOR_HCENTER_VCENTER);
		if (CCanvas.s_iWcanvas > CRess.imgTitle.getWidth()) {
			g.drawImage(CRess.imgTitle, 0, 0, CPosition.ANCHOR_LEFT_TOP);
			for (int i = CRess.imgTitle.getWidth(); i < CCanvas.s_iWcanvas
					+ CRess.imgBanner_back.getWidth(); i += CRess.imgBanner_back
					.getWidth())
				g.drawImage(CRess.imgBanner_back, i, 0,
						CPosition.ANCHOR_LEFT_TOP);

		} else {
			g.drawImage(CRess.imgTitle, 0, 0, CPosition.ANCHOR_LEFT_TOP);
		}
		for (int i = 0; i < CRess.imgDotted.length; i++)
			g.drawImage(CRess.imgDotted[i], CCanvas.s_iWcanvas - 30
					+ (i * (CRess.imgDotted[i].getWidth() + 5)), 10,
					CPosition.ANCHOR_LEFT_TOP);
	}

	public void paintMenuBar(Graphics g) {
		if (CCanvas.s_iWcanvas > CRess.menubar.getWidth()) {
			g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas,
					CPosition.ANCHOR_LEFT_BOTTOM);
			for (int i = CRess.menubar.getWidth(); i < CCanvas.s_iWcanvas
					+ CRess.footer_back.getWidth(); i += CRess.footer_back
					.getWidth())
				g.drawImage(CRess.footer_back, i, CCanvas.s_iHcanvas,
						CPosition.ANCHOR_LEFT_BOTTOM);

		} else {
			g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas,
					CPosition.ANCHOR_LEFT_BOTTOM);

		}
	}

	public void Paint(Graphics g) {
		g.setColor(0x000000);
		g.fillRect(0, 0, CCanvas.s_iWcanvas, CCanvas.s_iHcanvas);
		paintBackGround(g);
		paintItemApp(g);
		// g.setClip(0, 0, CCanvas.s_iWcanvas, CCanvas.s_iHcanvas);
		paintMenuBar(g);
		CCanvas.mediumfont.drawString(g, "Menu", 5, CCanvas.s_iHcanvas,CPosition.ANCHOR_LEFT_BOTTOM, 0xa1a6a1, 0x090b7a, true);
		CCanvas.s_Instan.connect.isPainting = true;

	}

	public int pointerItems(int x, int y) {
		int x0 = 0;
		int x1 = CCanvas.s_iWcanvas;
		int y0 = CConfig.HIEGHT_TITLE;
		int y1 = CCanvas.s_iHcanvas - CConfig.HIEGHT_TITLE
				- CConfig.HIEGHT_COMMNAND;
		int iRow = 0;
		int iCol = 0;
		if (x >= x0 && x <= x1 && y >= y0 && y <= y1) {
			for (int i = 0; i < elementItem; i++) {
				if (Item[i / colum][i % colum] != null) {
					int xx0 = Item[i / colum][i % colum].x;
					int yy0 = Item[i / colum][i % colum].y;
					if (x >= xx0 && x < xx0 + 57 && y >= yy0 && y < yy0 + 57) {
						// iRow = (i + 3) / 3 - 1;
						// iCol = i % 3;
						iRow = (i + colum) / colum - 1;
						iCol = i % colum;
						if (iRow == flagRow && iCol == flagColum) { // fire
							CCanvas.keyPressed[KeyConstant.KEY_FIRE] = true;
							return 0;
						}
						Item[flagRow][flagColum].isFocus = false;

						// scrolling
						if (iRow > flagRow) {
							flagRow = iRow;
							flagColum = iCol;
							DetectScrollItem(1);

						} else if (iRow < flagRow) {
							flagRow = iRow;
							flagColum = iCol;
							DetectScrollItem(0);
						}
						flagRow = iRow;
						flagColum = iCol;
						Item[flagRow][flagColum].isFocus = true;
						return 0;

					}
				}
			}
		}
		return -1;
	}

//	public void touchDrag(int x, int y) {
//		System.out.println("-- drag");
//	//	CCanvas. roll.move(x, y);
//		
//		//System.out.println("gdsaghdsvaghdasgh " + CCanvas.roll.getY());
//	}
//	public void touchDown(int x, int y) {
//		System.out.println("-- down");
//		//CCanvas. roll.moveStart(x, y);
//	}
//	public void touchUp(int x, int y, int dragTime, int xBegin, int yBegin) {
//		System.out.println("-- up");
//		//CCanvas. roll.moveFinish(x, y, dragTime, xBegin, yBegin);
//	}
	
	public void pointerPressed(int px, int py) {
		if (CCanvas.isPointerPressed) // User PointerPressed
		{
			if (Pointer.SelectSoftKey(px, py) == 0) // left softkey
			{
				CCanvas.keyPressed[KeyConstant.KEY_LEFT_SOFTKEY] = true;
			} else { // click into items
				pointerItems(px, py);
			}

		}
		
//		if(CCanvas.isdrag)
//		{
//			touchDrag(px, py);
//			//System.out.println("drag");
//			
//		}else if(CCanvas.istouchup)
//		{
//			touchUp(px, py, CCanvas.holdtime,CCanvas.xBegin, CCanvas.xBegin);
//			//System.out.println("up");
//		}else if(CCanvas.istouchdown)
//		{
//			touchDown(px, py);
//			//System.out.println("down");
//			
//		}
		
		CCanvas.resetPointerPress(); // User PointerReleased
	}

	public void UpdateKeyPress(boolean[] keyPressed, boolean[] keyHold,int currentAsccii[]) {
		
		if (CCanvas.s_Instan.connect.isPainting == true) {
			
			Update();
			
			if (keyPressed[KeyConstant.KEY_RIGHT]) // User PressKey Fire
			{
			//	System.out.println("phim phai");
				SwapFocus(0);
			} else if (keyPressed[KeyConstant.KEY_LEFT]) // User PressKey Fire
			{
				//System.out.println("phim trai");
				SwapFocus(1);
			} else if (keyPressed[KeyConstant.KEY_UP]) // User PressKey Fire
			{
				//System.out.println("phim len");
				SwapFocus(2);
			} else if (keyPressed[KeyConstant.KEY_DOWN]) // User PressKey Fire
			{
				//System.out.println("phim xuong");
				SwapFocus(3);
			} else if (keyPressed[KeyConstant.KEY_FIRE]) // User PressKey Fire
			{
				//System.out.println("phim fire");
				if (Item[flagRow][flagColum].isFocus == true) {
					CCanvas.s_Instan.connect.koclearurl = -1;
					CCanvas.s_Instan.LoadingState.timeForLoading = 50;
					CCanvas.s_Instan.LoadingState.time = CCanvas.s_Instan.LoadingState.fixtime;
					CCanvas.CurrentScreen = CCanvas.isChangeLoadingState;
					CCanvas.s_Instan.connect
							.setURL(Item[flagRow][flagColum].Link);
					
					// code moi them
					if(CCanvas.s_Instan.connect.Link.size() != 0)
						CCanvas.s_Instan.connect.Link.removeAllElements();
					
					CCanvas.s_Instan.connect.Link
							.addElement(Item[flagRow][flagColum].Name);
					CCanvas.s_Instan.connect.URL
							.addElement(Item[flagRow][flagColum].Link);
					CCanvas.s_Instan.connect.RequestServerGetData();
				}
			} else if (keyPressed[KeyConstant.KEY_LEFT_SOFTKEY]) {
				// ///////////////// create command and call leftsoftkey action
				CCanvas.submenu.initCommand();
				if (CCanvas.submenu.leftSoftKey != null) {
					CCanvas.submenu.leftSoftKey.action.perform();
				}
				CCanvas.CurrentScreen = CCanvas.isSubMenu;
			}
		}
		CCanvas.resetkey(); // User KeyReleased
	}

	public void ScrollItem() {
		if (Cry > 0) {
			for (int i = 0; i < elementItem; i++) {
				Item[i / colum][i % colum].y -= (Cry / NumberDivided + Cry
						% NumberDivided)
						* AmDuong;
			}
			Cry -= (Cry / NumberDivided + Cry % NumberDivided);
		} else {
			Cry = 0;
			PressOnce = false;
		}
	}

	public void Update() {
		ScrollItem();
		// Kiem tra co pointer khong?
		if (iUpDown != 0) {
			if (PressOnce && iUpDown > 0) {
				flagRow++;
			}
			if (iUpDown == 1) {
				for (int i = 0; i < iDUpDown; i++) {
					SwapFocus(2);
				}
				// CCanvas.keyPressed[KeyConstant.KEY_UP ]=true;
			} else if (iUpDown == 2) {
				for (int i = 0; i < iDUpDown; i++) {
					SwapFocus(3);
				}
			}

			iDUpDown = 0;
			iUpDown = 0;

		} else if (iUpDown == 0 && iLeftRight != 0) {
			if (iLeftRight == 1) {
				// CCanvas.keyPressed[KeyConstant.KEY_LEFT ]=true;
			} else if (iLeftRight == 2) {
				CCanvas.keyPressed[KeyConstant.KEY_RIGHT] = true;
			}
			iDLeftRight--;
			if (iDLeftRight <= 0) {
				iLeftRight = 0;
			}
		}
		// flagRow =
		// flagColum =
	}

	public void DetectScreenItem(int wcanvas, int hcanvas, int wImage,
			String[][] data) {
		int WidthImageOrWidthCanvas = 0;

		Item = new ItemApp[row][colum];
		for (int i = 0; i < elementItem; i++) {
			Item[i / colum][i % colum] = new ItemApp();
			Item[i / colum][i % colum].row = i / colum;
			Item[i / colum][i % colum].colum = i % colum;
			Item[i / colum][i % colum].NumberPage = 1;
			Item[i / colum][i % colum].Name = data[i][0];
			Item[i / colum][i % colum].Link = data[i][1];
			Item[i / colum][i % colum].indexcolor = 0;
			Item[i / colum][i % colum].indexborder = 0;
			Item[i / colum][i % colum].w = CRess.IconApp[i / colum][i % colum]
					.getWidth();
			Item[i / colum][i % colum].h = CRess.IconApp[i / colum][i % colum]
					.getHeight();
			Item[i / colum][i % colum].x = xStart + (i % colum)
					* (W_ITEMS + KC_ITEMS);
			Item[i / colum][i % colum].y = yStartItem + (i / colum)
					* yDistanceItem;
			Item[i / colum][i % colum].xFocus = 9;
			Item[i / colum][i % colum].yFocus = 9;
			Item[i / colum][i % colum].xName = Item[i / colum][i % colum].w
					/ 2
					- CCanvas.mediumfont
							.stringWidth(Item[i / colum][i % colum].Name) / 2;
			Item[i / colum][i % colum].yName = Item[i / colum][i % colum].h + 5;
		}
		Item[0][0].isFocus = true;
		data = null;
		System.gc();
	}

	public void DetectScrollItem(int UpDown) {
		if (UpDown == 0) // Up
		{
			if ((Item[flagRow][flagColum].y < yStartItem && flagRow == 0 && Cry == 0)
					|| (Item[flagRow][flagColum].y < yStartItem + 20 && flagRow != 0)
					&& Cry == 0) {
				Cry += yDistanceItem;
				AmDuong = -1;
			}
		} else // Down
		{
			// System.out.println("flagRow < row - 1:"+(flagRow < row - 1));
			// System.out.println("Item[flagRow][flagColum].y+Item[flagRow][flagColum].h > LowerLimit-40:"+(Item[flagRow][flagColum].y+Item[flagRow][flagColum].h
			// > LowerLimit-40));
			// System.out.println("Item[flagRow][flagColum].y>=LowerLimit-40:"+(Item[flagRow][flagColum].y>=LowerLimit-40));
			// System.out.println("Cry == 0:"+ (Cry == 0));
			// if ((flagRow < row - 1 &&
			// Item[flagRow][flagColum].y + Item[flagRow][flagColum].h >
			// CCanvas.s_iHcanvas - CConfig.HIEGHT_COMMNAND)
			// || (Item[flagRow][flagColum].y > LowerLimit + 10) && Cry == 0) {
			if ((flagRow < row - 1 && Item[flagRow][flagColum].y
					+ Item[flagRow][flagColum].h > LowerLimit - 40)
					|| (Item[flagRow][flagColum].y >= LowerLimit - 40)
					&& Cry == 0) {
				Cry += yDistanceItem;
				AmDuong = 1;
			}
		}
	}

	public void SwapFocus(int Huong) {
		for (int i = 0; i < elementItem; i++) {
			if (Item[i / colum][i % colum].isFocus == true) {
				Item[i / colum][i % colum].isFocus = false;
				break;
			}
		}
		if (PressOnce == false) {
			if (Huong == 0) // Right
			{
				flagColum++; // tang cot
				if (flagColum > colum - 1) // xuong hang
				{
					if (flagRow < row - 1) {
						flagColum = 0; // reset cot
						flagRow++; // tang hang
					} else {
						flagColum--;
					}
				} else if (Item[flagRow][flagColum] == null) // null thi tru lai
																// ko cho ditiep
				{
					flagColum--;
				}
				DetectScrollItem(1);
			} else if (Huong == 1) // Left
			{
				flagColum--;
				if (flagColum < 0) {
					flagColum = colum - 1;
					flagRow--;
					if (flagRow < 0) {
						flagRow = flagColum = 0;
					}
				}
				DetectScrollItem(0);
			} else if (Huong == 2) // Up
			{
				if (flagRow > 0 && Cry == 0) {
					flagRow--;
				}
				DetectScrollItem(0);
			} else if (Huong == 3) // Down
			{

				if (flagRow < row - 1 && Cry == 0) {
					flagRow++;
				}
				if (Item[flagRow][flagColum] == null) {
					for (int j = flagColum; j >= 0; j--) {
						if (Item[flagRow][j] != null) {
							flagColum = j;
							break;
						}
					}
				}
				DetectScrollItem(1);
			}
			PressOnce = true;

		}
		Item[flagRow][flagColum].isFocus = true;
	}

	public void resetDataPage1() {
		CRess.IconApp = null;
		CRess.imgFocus = null;
		Item = null;
		System.gc();
	}

}
